package com.jxc.servlet.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie/loginServlet")
public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print("<h1>用户登陆成功</h1>");

        // 实例化一个新的cookie对象，并赋予name，value属性
        Cookie cookie = new Cookie("user","jxc");
        // 设置cookie保存的时间，以秒做单位
        cookie.setMaxAge(60*60*24*7);
        // 将创建好的cookie通过response的addCookie方法添加到Cookie中
        resp.addCookie(cookie);
    }
}
